package com.enjoy.xbase.plug;

import android.os.CountDownTimer;


/**
 * 描述：定时器
 */
public class CountTimer {

    CountDownTimer mTimer;//计时器
    TimerListener listener;

    public void startCountTimer(long millisInFuture, long countDownInterval, TimerListener listener) {
        this.listener = listener;
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        mTimer = new CountDownTimer(millisInFuture, countDownInterval) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (CountTimer.this.listener != null)
                    CountTimer.this.listener.onTick(millisUntilFinished);
            }

            @Override
            public void onFinish() {
                if (CountTimer.this.listener != null)
                    CountTimer.this.listener.onFinish();
            }
        };
        mTimer.start();
    }

    public void stopCountTimer() {
        this.listener = null;
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    public interface TimerListener {
        void onTick(long millisUntilFinished);

        void onFinish();
    }
}
